package com.city.service.report.impl;

import com.city.domain.city.CityTown;
import com.city.domain.city.CityVillage;
import com.city.domain.report.*;
import com.city.repository.city.CityTownRepository;
import com.city.repository.city.CityVillageRepository;
import com.city.repository.report.*;
import com.city.service.report.ReportService;
import com.city.helper.AuthHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @deprecation: 上报服务
 * @email: leichuqiao@126.com
 * @author: 雷楚桥
 * @date: 2018/7/23 13:22
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private CityTownRepository townR;
    @Autowired
    private CityVillageRepository villageR;
    @Autowired
    private CtEventRepository eventR;
    @Autowired
    private CtReportRepository reportR;
    @Autowired
    private CtCategoryRepository categoryR;
    @Autowired
    private CtBranchRepository branchR;
    @Autowired
    private CtDoBanRepository doBanR;
    @Autowired
    private CtBackRepository backR;
    @Autowired
    private CtCopnoteRepository copnoteR;

    @Autowired
    private AuthHelper authH;



    @Override
    public List<CityTown> findAllCityTown() {
        return townR.findAll();
    }

    @Override
    public Integer findAllByScheduleAndMonth(Integer schedule, Integer year, Integer month) {
        return reportR.findAllByScheduleAndMonth(schedule, year, month);
    }

    @Override
    public Integer findAllByScheduleAndDay(Integer schedule) {
        return reportR.findAllByScheduleAndDay(schedule);
    }

    @Override
    public Integer findAllByScheduleAndYear(Integer schedule, Integer year) {
        return reportR.findAllByScheduleAndYear(schedule,year);
    }

    @Override
    public String[] findAllByTypeBig() {
        return reportR.findAllByTypeBig();
    }

    @Override
    public List<String> findAllByYearAndBranchIdAndSchedule(Integer year,Integer branch_id,Integer schedule){
        return reportR.findAllByYearAndBranchIdAndSchedule(year,branch_id,schedule);
    }

    @Override
    public Integer findAllByYearAndTownId(Integer year, Integer townId) {
        return reportR.findAllByYearAndTownId(year,townId);
    }

    @Override
    public List<Integer> findAllYear() {
        return reportR.findAllYear();
    }

    @Override
    public Integer findAllByYearAndScheduleAndTownId(Integer year, Integer schedule, Integer townId) {
        return reportR.findAllByYearAndScheduleAndTownId(year,schedule,townId);
    }

    @Override
    public List<String> findAllByYearAndSchedule(Integer year, Integer schedule) {
        return reportR.findAllByYearAndSchedule(year,schedule);
    }

    @Override
    public List<String> findAllByYear(Integer year) {
        return reportR.findAllByYear(year);
    }

    @Override
    public List<CtReport> findAllByIdLimit(Integer left, Integer right) {
        return reportR.findAllByIdLimit(left,right);
    }


    @Override
    public Integer findAllByTownId(Integer town) {
        return reportR.findAllByTownId(town);
    }

    @Override
    public Integer findAllByScheduleAndTownId(Integer status,Integer town) {
        return reportR.findAllByScheduleAndTownId(status,town);
    }

    @Override
    public Integer findAllByBranchId(Integer branchId) {
        return reportR.countAllByBranchId(branchId);
    }

    @Override
    public Integer findAllByBranchIdAndSchedule(Integer branchId, Integer schedule) {
        return reportR.findAllByBranchIdAndSchedule(branchId,schedule);
    }


    @Override
    public List<CityVillage> findCityVillageByTownId(Integer townId) {
        return villageR.findAllByTownId(townId);
    }

    @Override
    public CtReport saveCtReport(CtReport ctReport) {
        return reportR.save(ctReport);
    }

    @Override
    public CtReport saveCtReport(Integer id, Integer schedule) {
        CtReport ctReport = reportR.findOne(id);
        ctReport.setSchedule(schedule);
        if(schedule.equals(1)){
            //保存是哪个受理员进行案件受.理的
            Integer handManId = authH.thisUserId();
            System.out.println("HandManId: " + handManId);
            ctReport.setHandleManId(handManId);
        }
        ctReport = reportR.save(ctReport);
        return ctReport;
    }

    @Override
    public List<CtEvent> findAllCtEventByCategoryIdAndParent(Integer categoryId, Integer parent) {
        return eventR.findAllByCategoryIdAndParent(categoryId,parent);
    }

    @Override
    public List<CtReport> findAllReport() {
        return reportR.findAll();
    }

    @Override
    public CtCategory findCategoryById(Integer categoryId) {
        return categoryR.findOne(categoryId);
    }

    @Override
    public CtEvent findEventById(Integer typeBig) {
        return eventR.findOne(typeBig);
    }

    @Override
    public CityTown findTownById(Integer townId) {
        return townR.findOne(townId);
    }

    @Override
    public CityVillage findVillageById(Integer villageId) {
        return villageR.findOne(villageId);
    }

    @Override
    public CtReport findReportById(Integer id) {
        return reportR.findOne(id);
    }

    @Override
    public List<CtReport> findAllReportByReportId(Integer reportId) {
        return reportR.findALlReportByReportId(reportId);
    }

    @Override
    public List<CtBranch> findAllBranchByParent(Integer parent) {
        return branchR.findAllByParent(parent);
    }

    @Override
    public void reportDispatchBranch(Integer reportId, Integer branchId, Integer schedule) {
        reportR.dispatchBranch(reportId,branchId,schedule);
    }

    @Override
    public CtBranch findBranchById(Integer branchId) {
        return branchR.findOne(branchId);
    }

    @Override
    public void reportDelById(Integer id) {
        reportR.delete(id);
    }

    @Override
    public List<CtReport> findAllReportByScheduleLimit(Integer left, Integer right) {
        return reportR.findAllByScheduleLimit(left,right);
    }

    @Override
    public List<CtReport> findAllReportByScheduleByUserId(Integer left, Integer right, Integer branchId) {
        return reportR.findAllReportByScheduleByUserId(left, right, branchId);
    }

    @Override
    public boolean checkDoBan(Integer id) {
        return doBanR.findByReportId(id) != null;
    }

    @Override
    public boolean checkDoBan(Integer reportId, Integer manId) {
        List<CtDoBan> doBanList = doBanR.findByReportIdAndManId(reportId,manId);
        return !doBanList.isEmpty();
    }

    @Override
    public CtReport cancelDoBan(Integer id) {
        CtReport ctReport = reportR.findOne(id);
        doBanR.deleteByReportIdAndManId(id,authH.thisUserId());
        return ctReport;
    }

    @Override
    public CtDoBan saveDoBan(CtDoBan doBan) {
        return doBanR.save(doBan);
    }

    @Override
    public List<CtDoBan> findAllDoBanByReportId(Integer reportId) {
        return doBanR.findAllByReportId(reportId);
    }

    @Override
    public List<CtDoBan> findAllDoBanByReportIdOrderByTimeDesc(Integer reportId) {
        return doBanR.findAllByReportIdOrderByTimeDesc(reportId);
    }

    @Override
    public List<CtDoBan> findAllDoBanByLightEqZero() {
        return doBanR.findAllByLightEqZero();
    }

    @Override
    public CtBack saveBack(CtBack back) {
        return backR.save(back);
    }

    @Override
    public CtCopnote saveCopnote(CtCopnote copnote) {
        return copnoteR.save(copnote);
    }

    @Override
    public List<CtCopnote> findAllCopnote() {
        return copnoteR.findAll();
    }

    @Override
    public CtCopnote findCopnoteById(Integer id) {
        return copnoteR.findOne(id);
    }

    @Override
    public List<CtReport> findAllReportByBranchId(Integer branchId) {
        return reportR.findAllByBranchId(branchId);
    }

    @Override
    public List<CtBranch> findAll() {
        return branchR.findAll();
    }

    @Override
    public void dispatchPeople(Integer reportId, Integer peopleId, Integer schedule) {
        reportR.dispatchPeople(reportId,peopleId,schedule);
    }

    @Override
    public void reWork(Integer reportId, Integer isrework, Integer schedule) {
        reportR.reWork(reportId,isrework,schedule);
    }

    @Override
    public void caseBack(Integer reportId, Integer isrework, Integer schedule) {
        reportR.caseBack(reportId,isrework,schedule);
    }

    @Override
    public void checkPass(Integer reportId, Integer hechaId, Integer schedule) {
        reportR.checkPass(reportId,hechaId,schedule);
    }

    @Override
    public void checkAccepted(Integer reportId, Integer finishId, Integer schedule) {
        reportR.checkAccepted(reportId,finishId,schedule);
    }

    @Override
    public void updateDoBan(Integer doban, Integer id) {
        reportR.updateDoBan(doban, id);
    }

    @Override
    public void updateEndTime(String endTime, Integer id) {
        reportR.updateEndTime(endTime, id);
    }

    @Override
    public void updateIsOntime(Integer id) {
        reportR.updateIsOntime(id);
    }
}
